﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace GameJam2012 {
    public class Particle {

        private int lifeTime;
        private bool alive;
        private bool applyGravity;
        private float size;
        private Random random;
        private Color color;
        private Vector2 gravity;
        private Vector2 position;
        private Vector2 velocity;

        public Particle(Vector2 position,Vector2 velocity, Color color, bool applyGravity, float size) {
            random = new Random();
            this.position = position;
            this.velocity = velocity;
            this.color = color;
            this.applyGravity = applyGravity;
            this.size = (float)(random.NextDouble() * size);
            alive = true;
            gravity = new Vector2(0.0f, 0.5f);
            lifeTime = random.Next(50,100);
        }

        public void Update() {
            if (alive) {
                if(applyGravity)
                    velocity += gravity;
                position += velocity;
                size = size - (size / 10);
                lifeTime--;
                if(lifeTime <= 0)
                    alive = false;
            }
        }

        public void Draw(SpriteBatch spriteBatch, Texture2D particle, Vector2 offset) {
            if(alive){
                Rectangle sourceRect = new Rectangle(0, 0, particle.Width, particle.Height);
                spriteBatch.Draw(particle, position + offset, sourceRect, color, 0.0f, 
                                 Vector2.Zero, size, SpriteEffects.None, .8f);
            }
        }

        public bool IsAlive() {
            return alive;
        }
    }
}
